package com.ruoyi.ams.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.core.domain.model.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import java.util.*;

/**
 * @Description
 * @Author YangJun
 * @Date 2021年11月23日 10:38:52
 **/
//@Async
@Service
public class AmsAsyncService {

    @Autowired
    private IAmsAssetInfoService amsAssetInfoService;
    @Autowired
    private IAmsAssetTypeService amsAssetTypeService;
    @Autowired
    private IAmsAssetBrandService amsAssetBrandService;
    @Autowired
    private IAmsAssetProjectService amsAssetProjectService;
    @Autowired
    private IAmsOwnAddressService amsOwnAddressService;
    @Autowired
    private IAmsAssetTemplateService amsAssetTemplateService;
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public ImportAssetInfoVo getTypeAndTemplate(ImportAssetInfoVo vo, Map<String, AmsAssetType> typeMap, List<FullTypeInfoVo> typeNames, String userName) {

        Map<String, AmsAssetTemplate> templateMap = new HashMap<>();
        List<AmsAssetTemplate> saveTemplate = new ArrayList<>();
        Set<String>unique =new HashSet<>();
        for (FullTypeInfoVo fullTypeInfoVo : typeNames) {
            Long assetTypeId = typeMap.get(fullTypeInfoVo.getAssetTypeName()).getAssetTypeId();
            AmsAssetTemplate templateIsExit = amsAssetTemplateService.getOne(new QueryWrapper<AmsAssetTemplate>()
                    .eq(AmsAssetTemplate.ASSET_TYPE_ID, assetTypeId).eq(AmsAssetTemplate.ASSET_TEMPLATE_NAME, fullTypeInfoVo.getTemplateName()), false);
            if (templateIsExit != null) {
                templateMap.put(fullTypeInfoVo.getTemplateName(), templateIsExit);
            }else {
                if(!unique.contains(assetTypeId+"_"+fullTypeInfoVo.getTemplateName())){
                    AmsAssetTemplate template = new AmsAssetTemplate();
                    template.setAssetTypeId(assetTypeId);
                    template.setAssetTemplateName(fullTypeInfoVo.getTemplateName());
                    template.setMeasureUnit(fullTypeInfoVo.getMeasureUnit());
                    template.setSpecification(fullTypeInfoVo.getSpecification());
                    template.setUpdateBy(userName);
                    template.setCreateBy(userName);
                    template.setCreateTime(System.currentTimeMillis());
                    template.setUpdateTime(System.currentTimeMillis());
                    saveTemplate.add(template);
                    unique.add(assetTypeId+"_"+fullTypeInfoVo.getTemplateName());
                }
            }
        }
        amsAssetTemplateService.saveBatch(saveTemplate);
        saveTemplate.forEach(item -> templateMap.put(item.getAssetTemplateName(), item));
        vo.setTemplateNames(templateMap);
        return vo;
    }
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void getBrands(ImportAssetInfoVo vo, Set<String> brandNames, String userName) {

        if (brandNames.size() > 0) {
            Map<String, AmsAssetBrand> brandMap = new HashMap<>();

            List<AmsAssetBrand> brands = amsAssetBrandService.list(new QueryWrapper<AmsAssetBrand>().in(AmsAssetBrand.BRAND_NAME, brandNames));
            if (brands.size() > 0) {
                List<String> hasBrands = new ArrayList<>();
                brands.forEach(item -> {
                    hasBrands.add(item.getBrandName());
                    brandMap.put(item.getBrandName(), item);

                });
                brandNames.removeAll(hasBrands);
            }

            List<AmsAssetBrand> addBrands = new ArrayList<>();
            brandNames.forEach(item -> {
                AmsAssetBrand amsAssetBrand = new AmsAssetBrand();
                amsAssetBrand.setBrandName(item);
                amsAssetBrand.setCreateBy(userName);
                amsAssetBrand.setCreateTime(System.currentTimeMillis());
                amsAssetBrand.setUpdateBy(userName);
                amsAssetBrand.setUpdateTime(System.currentTimeMillis());
                addBrands.add(amsAssetBrand);
                brandMap.put(item, amsAssetBrand);
            });
            amsAssetBrandService.saveBatch(addBrands);
            vo.setBrandNames(brandMap);
        }
    }
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void getProjects(ImportAssetInfoVo vo, Set<String> projectNames, String userName) {

        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        if (projectNames.size() > 0) {
            Map<String, AmsAssetProject> projectMap = new HashMap<>();
            List<AmsAssetProject> projects = amsAssetProjectService.list(new QueryWrapper<AmsAssetProject>().in(AmsAssetProject.PROJECT_NAME, projectNames));
            if (projects.size() > 0) {
                List<String> hasProjects = new ArrayList<>();
                projects.forEach(item -> {
                    hasProjects.add(item.getProjectName());
                    projectMap.put(item.getProjectName(), item);

                });
                projectNames.removeAll(hasProjects);
            }
            List<AmsAssetProject> addProjects = new ArrayList<>();
            projectNames.forEach(item -> {
                AmsAssetProject amsAssetProject = new AmsAssetProject();
                amsAssetProject.setProjectName(item);
                amsAssetProject.setCreateBy(userName);
                amsAssetProject.setUpdateBy(userName);
                amsAssetProject.setUpdateTime(System.currentTimeMillis());
                amsAssetProject.setCreateTime(System.currentTimeMillis());
                addProjects.add(amsAssetProject);
                projectMap.put(item, amsAssetProject);
            });
            amsAssetProjectService.saveBatch(addProjects);
            vo.setProjectNames(projectMap);
        }

    }
}
